<?php

class ProfileDocumentTypeConfigurationDao extends BaseDao
{

    public function saveProfileDocumentType(ProfileDocumentType $profileDocumentType)
    {

        try {
            $profileDocumentType->save();
            return $profileDocumentType;
        } catch (Exception $e) {
            throw new DaoException($e->getMessage(), $e->getCode(), $e);
        }

    }

    public function getProfileDocumentType($id)
    {

        try {
            return Doctrine::getTable('ProfileDocumentType')->find($id);
        } catch (Exception $e) {
            throw new DaoException($e->getMessage(), $e->getCode(), $e);
        }

    }

    public function getProfileDocumentTypeByCode($code)
    {

        try {

            $q = Doctrine_Query::create()
                ->from('ProfileDocumentType')
                ->where('code = ?', trim($code));

            return $q->fetchOne();

        } catch (Exception $e) {
            throw new DaoException($e->getMessage(), $e->getCode(), $e);
        }

    }

    public function getProfileDocumentTypeList($sortBy = 'code', $sortOrder = 'ASC', $activeOnly = true)
    {

        $sortBy = ($sortBy == "") ? 'code' : $sortBy;
        $sortOrder = ($sortOrder == "") ? 'ASC' : $sortOrder;

        try {
            $q = Doctrine_Query :: create()
                ->from('ProfileDocumentType');
            if ($activeOnly == true) {
                $q->addWhere('isDeleted = ?', ProfileDocumentType::ACTIVE);
            }
            $q->orderBy($sortBy . ' ' . $sortOrder);

            return $q->execute();
        } catch (Exception $e) {
            throw new DaoException($e->getMessage());
        }

    }

    public function deleteProfileDocumentTypes($toBeDeletedProfileDocumentTypeIds)
    {

        try {
            $q = Doctrine_Query :: create()
                ->update('ProfileDocumentType')
                ->set('isDeleted', '?', ProfileDocumentType::DELETED)
                ->whereIn('id', $toBeDeletedProfileDocumentTypeIds);

            return $q->execute();

        } catch (Exception $e) {
            throw new DaoException($e->getMessage(), $e->getCode(), $e);
        }

    }

    public function isExistingProfileDocumentTypeCode($profileDocumentTypeCode)
    {

        try {

            $q = Doctrine_Query:: create()->from('ProfileDocumentType rm')
                ->where('rm.code = ?', trim($profileDocumentTypeCode));

            if ($q->count() > 0) {
                return true;
            }

            return false;

        } catch (Exception $e) {
            throw new DaoException($e->getMessage(), $e->getCode(), $e);
        }

    }

}
